import UpdateInvesterPortfolio from '../../common/update-invester-portfolio/update-invester-portfolio.vue'
import UpdateInvesterBuyType from '../../common/update-invester-buy-type/update-invester-buy-type.vue'
import { swapInvesterbeta, getInvesterBuyTypeList, setHkStatus, deleteInvester, showMsg } from '../../../../../vuex/action.js'
import extend from 'extend'

export default {
  name: 'InvesterPortfolioItem',
  data() {
    return {
    }
  },
  props: ['detail', 'index', 'list'],
  methods: {
    showMsg(msg) {
      this.xShowMsg({
        type: 'danger',
        content: msg
      })
    },
    deepClone(obj) {
      return extend(true, {}, obj)
    },
    moveUp(index) {
      let fromBetaId = this.list[index].id
      let toBetaId = this.list[index - 1].id
      this.swapInvesterbeta(fromBetaId, toBetaId)
    },
    moveDown(index) {
      let fromBetaId = this.list[index].id
      let toBetaId = this.list[index + 1].id
      this.swapInvesterbeta(fromBetaId, toBetaId)
    },
    swapInvesterbeta(fromBetaId, toBetaId) {
      this.xSwapInvesterbeta({
        id1: fromBetaId,
        id2: toBetaId,
        type: this.payType
      }).then((res) => {
        this.$dispatch('on_refresh_list')
      }).catch((res) => {
        this.showMsg(res.error_msg)
      })
    },
    evtSetStatus(detail) {
      // 上架需先检查套餐，下架可以直接下架
      if (detail.status === 1) {
        this.setStatus(detail)
      } else {
        this.setOnShelves(detail)
      }
    },
    setOnShelves(detail) {
      this.xGetInvesterBuyTypeList({
        id: detail.id
      }).then((res) => {
        let buytypeList = res.data.list
        if (buytypeList && buytypeList.length > 0) {
          this.setStatus(detail)
        } else {
          this.showMsg('请先设置付费套餐')
        }
      }).catch((res) => {
        this.showMsg(res.error_msg)
      })
    },
    setStatus(detail) {
      let params = {
        id: +detail.id,
        status: +detail.status === 1 ? 0 : 1
      }

      this.xSetHkStatus(params)
        .then((res) => {
          this.$dispatch('on_refresh_list')
        }).catch((res) => {
          this.showMsg(res.error_msg)
        })
    },
    deleteInvester(detail) {
      this.xDeleteInvester({id: detail.id})
        .then(() => {
          this.xShowMsg({
            type: 'info',
            content: '删除' + detail.name + '组合成功'
          })
          this.$dispatch('on_refresh_list')
        }).catch((res) => {
          this.showMsg(res.error_msg)
        })
    }
  },
  // events: {
  //   on_refresh_list() {
  //     console.log('refresh')
  //     return false
  //   }
  // },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xGetInvesterBuyTypeList: getInvesterBuyTypeList,
      xSetHkStatus: setHkStatus,
      xDeleteInvester: deleteInvester,
      xSwapInvesterbeta: swapInvesterbeta
    },
    getters: {
      payType: state => state.payType
    }
  },
  components: {
    'update-invester-buy-type': UpdateInvesterBuyType,
    'update-invester-portfolio': UpdateInvesterPortfolio
  },
  ready() {
    // this.$dispatch('on_refresh_list')
  }
}
